<template>
  <div class="container">
    <div class="header1" style="padding: 10px 0px">
      <div style="display: flex; justify-content: space-between">
        <div style="display: flex; justify-content: start">
          <!-- <a-button class="hte-btn" plain @click="handleExport">导出</a-button> -->
          <a-button class="hte-btn" plain @click="handleReload">刷新</a-button>
          <a-button class="hte-btn" plain @click="showInitForm = true">初始化流程步骤</a-button>
        </div>
        <div style="display: flex; justify-content: end">
          <a-input-search v-model="searchForm.keyword" @search="handleToSearch" placeholder="请按照关键字进行搜索" />
          <a-button class="hte-btn del-btn" @click="showSearchForm = true">高级查询</a-button>
        </div>
      </div>
      <a-modal
        :visible="showSearchForm"
        width="70vw"
        :bodyStyle="{ padding: '10px' }"
        title="高级查询"
        @ok="handleToSearch"
        @cancel="showSearchForm = false"
      >
        <a-form-model
          ref="ruleForm"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          @keyup.enter.native="handleToSearch"
        >
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="流程状态">
                <a-select v-model="searchForm.status" style="width: 100%" allowClear>
                  <a-select-option value=""> 全部 </a-select-option>
                  <a-select-option v-for="(item) in codes.status" :value="item.code" :key="item.code">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="流程Key">
                <a-input allow-clear v-model="searchForm.flowKey" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="业务主键">
                <a-input allow-clear v-model="searchForm.businessKey" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-modal>
    </div>
    <div class="content-list">
      <a-table
        :columns="columns"
        :data-source="records"
        rowKey="id"
        bordered
        :pagination="false"
        :customRow="customRow"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
      </a-table>
      <a-pagination
        show-size-changer
        :default-current="1"
        :total="searchForm.total"
        @change="pageChange"
        @showSizeChange="onShowSizeChange"
        :show-total="(total) => `共 ${total} 条记录`"
      />
    </div>
    <a-modal
      :forceRender="true"
      title="Json查看器"
      :visible="visible"
      :closable="false"
      @ok="visible = false"
      @cancel="visible = false"
      width="80vw"
      height="60vh"
    >
      <JsonEditor ref="jsonEditor" v-model="json"> </JsonEditor>
      <template slot="footer">
        <a-button type="primary" @click="handleSave">保存修改</a-button>
        <a-button @click="visible = false">直接关闭</a-button>
      </template>
    </a-modal>
    <a-modal
      :forceRender="true"
      title="初始化流程数据"
      :visible="showInitForm"
      :closable="false"
      @ok="showInitForm = false"
      @cancel="showInitForm = false"
      width="40vw"
      height="40vh"
    >
      <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :span="16">
            <a-form-model-item label="流程状态">
              <a-select v-model="initForm.code" style="width: 100%" allowClear>
                <a-select-option v-for="(item) in codes['TaskStepInfo初始化配置']" :value="item.code" :key="item.code">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-button type="primary" @click="handleInitStepInfo">执行</a-button>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24"> 若选项中未找到需要初始化的流程,请在字典表【TaskStepInfo初始化配置】配置 </a-col>
          <br>附加字段1:数据实体实例
          <br>附加字段1:流程实例ID字段
        </a-row>
        <a-row :gutter="24">
          <a-col :span="16">
            <a-form-model-item label="流程实例ID">
              <a-textarea
                      v-model="initForm.processInstanceIds"
                      placeholder="流程实例ID"
                      :auto-size="{ minRows: 1, maxRows: 10 }"
                      style="margin-top: 5px"
                    />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-button type="primary" @click="handleInitStepInfoByProcessIds">执行</a-button>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="24"> 流程实例ID可从流程控制处获取 </a-col>
        </a-row>
      </a-form-model>

      <template slot="footer">
        <a-button @click="showInitForm = false">关闭</a-button>
      </template>
    </a-modal>
  </div>
</template>
<script>
import { postAction } from '@/api/manage'
import { parseTime } from '@/utils/util'
import { UtilsMixin } from '@/mixins/UtilsMixin'
import { TableMixin } from '@/mixins/TableMixin'
import { CodesMixin } from '@/mixins/CodesMixin'
export default {
  mixins: [UtilsMixin, TableMixin, CodesMixin],
  components: {
    HomeNav: () => import('@/components/layouts/HomeNav'),
    JsonEditor: () => import('@/components/tools/JsonEditor'),
  },
  data() {
    return {
      visible: false,
      currUserId: this.$store.getters.userInfo.username,
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractReportInfo',
      },
      urls: {
        list: '/hteFlowStepInfo/page',
        edit: '/hteFlowStepInfo/edit',
        initByCode: '/hteFlowStepInfo/initByCode',
        initStepInfo: '/hteFlowStepInfo/initStepInfo',
      },
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      keywordType: '1',
      json: {},
      record: {},
      records: [],
      columns: [
        {
          title: '状态',
          dataIndex: 'statusStr',
          width: '60px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '流程KEY',
          dataIndex: 'flowKey',
          width: '200px',
          align: 'left',
          ellipsis: true,
        },
        {
          title: '流程实例ID',
          dataIndex: 'id',
          width: '200px',
          align: 'left',
          ellipsis: true,
        },
        {
          title: '业务编码',
          dataIndex: 'businessKey',
          width: '150px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '节点名称',
          dataIndex: 'taskName',
          width: '150px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '节点标识',
          dataIndex: 'taskKey',
          width: '150px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '当前处理人',
          dataIndex: 'userName',
          width: '150px',
          align: 'center',
          ellipsis: true,
        },
        {
          title: '接收时间',
          dataIndex: 'acceptTime',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          width: '100px',
          ellipsis: true,
        },
      ],
      showSearchForm: false,
      selectedRowKeys: [],
      selectedRows: [],
      initForm:{},
      showInitForm: false,
      status: [
        { code: '0', name: '草稿' },
        { code: '1', name: '启动' },
        { code: '2', name: '运行中' },
        { code: '3', name: '正常结束' },
        { code: '4', name: '终止' },
        { code: '5', name: '废弃' },
        { code: '6', name: '重启发起' },
      ],
    }
  },
  async mounted() {
    await this.fetchCodes(['TaskStepInfo初始化配置'])
    this.codes['status'] = this.status
    this.fetchList()
  },
  methods: {
    fetchList() {
      postAction(this.urls.list, this.searchForm).then((res) => {
        if (res.success) {
          this.records = res.result.records
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('查询数据错误:' + res.message)
        }
      })
    },

    handleReload() {
      this.fetchList()
    },
    handleToSearch() {
      this.searchForm.pageNo = 1
      this.showSearchForm = false
      this.fetchList()
    },
    handleExport() {
      this.searchForm.selectedIds = this.selectedRowKeys.join(',')
      let url = process.env.VUE_APP_API_BASE_URL + '/infrastructure/export?1=1'
      this.handleExportXls(parseTime(new Date(), '{y}{m}{d}') + '《流程当前步骤列表》', url, this.searchForm)
    },
    pageChange(pageNo, pageSize) {
      this.$set(this.searchForm, 'pageSize', pageSize)
      this.$set(this.searchForm, 'pageNo', pageNo)
      this.fetchList()
    },
    onShowSizeChange(pageNo, pageSize) {
      this.searchForm.pageNo = 1
      this.searchForm.pageSize = pageSize
      this.fetchList()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    customRow(record) {
      return {
        on: {
          dblclick: (event) => {
            if (!this.selectedRowKeys) {
              this.selectedRowKeys = []
              this.selectedRows = []
            }
            if (this.selectedRowKeys.indexOf(record.id) <= -1) {
              this.selectedRows.push(record)
              this.selectedRowKeys.push(record.id)
            } else {
              let index = this.selectedRowKeys.indexOf(record.id)
              this.selectedRowKeys.splice(index, 1)
              this.selectedRows.splice(index, 1)
            }
          },
        },
      }
    },
    handleInitStepInfo(){
      if(!this.initForm.code){
        this.$message.error('请先选择需要初始化的流程')
      }else{
        postAction(this.urls.initByCode+"?code="+this.initForm.code, {}).then((res) => {
        if (res.success) {
            this.$message.success('初始化成功')
            this.fetchList()
          } else {
            this.$message.error('初始化失败:' + res.message)
          }
        })
      }
    },
    handleInitStepInfoByProcessIds(){
      if(!this.initForm.processInstanceIds){
        this.$message.error('请先输入需要初始化的流程实例ID')
      }else{
        postAction(this.urls.initStepInfo,this.initForm.processInstanceIds.split(",") ).then((res) => {
        if (res.success) {
            this.$message.success('初始化成功')
            this.fetchList()
          } else {
            this.$message.error('初始化失败:' + res.message)
          }
        })
      }
    },
    
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';

.container {
  .content {
    margin-top: 20px;

    section {
      margin-bottom: 18px;

      .section-table {
        .add-btn {
          margin-left: 0;
          margin-bottom: 10px;
        }

        /deep/ .a-upload__input {
          display: none !important;
        }
      }
    }
  }

  .tips-box {
    margin-top: 32px;

    .add-btn {
      width: 180px;
      height: 40px;
      line-height: 40px;
      background-color: #4095e4;
      border-radius: 2px;
    }
  }
}

/deep/ .el-upload__input {
  display: none !important;
}
</style>
